//= require rails-ujs
//= require activestorage
//= require tabler
//= require tabler.plugins
//= require bootstrap-datepicker/core
//= require bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN
//= require admin/module
//= require admin/hotkeys
//= require admin/uploader
//= require admin/simditor
//= require select2.full.min
//= require Chart.bundle.min
//= require qrcode.min
//= require turbolinks

window.chinaRegionFu = window.chinaRegionFu || {};

function domReady() {
  $('body').off('change', '.china-region-select').on('change', '.china-region-select', function(event) {
    let $self = $(event.currentTarget);
    let $subRegionDom = $('[data-region-name="' + $self.data('sub-region') + '"]');
    let subName = $self.data('sub-region');
    let parentName = $self.data('region-name');
    let parentId = $self.val();
    if ($subRegionDom.length > 0 && subName && parentName && parentId) {
      $.getJSON('/china_region_fu/fetch_options', {
          columns: window.chinaRegionFu.fetchColumns || 'id,name',
          sub_name: subName,
          parent_name: parentName,
          parent_id: parentId
        }, function(json) {
          if (window.chinaRegionFu.ajaxDone) {
            window.chinaRegionFu.ajaxDone(json);
          } else {
            var options = [];
            $self.parent().nextAll().find('.china-region-select > option[value!=""]').remove()
            options.push("<option value=''>请选择</option>");
            $.each(json.data, function(_, value) {
              options.push("<option value='" + value.id + "'>" + value.name + "</option>");
            });
            $subRegionDom.empty();
            $subRegionDom.append(options.join(''));
          }
      }).fail(function(xhr, textStatus, error) {
        window.chinaRegionFu.ajaxFail && window.chinaRegionFu.ajaxFail(xhr, textStatus, error);
      }).always(function(event, xhr, settings) {
        window.chinaRegionFu.ajaxAlways && window.chinaRegionFu.ajaxAlways(event, xhr, settings);
      });
    }
  });

  $('#add_option_value_dialog').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget) // Button that triggered the modal
    var name = button.data('option-type-name') // Extract info from data-* attributes
    var id = button.data('option-type-id')
    // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
    // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
    var modal = $(this)
    modal.find('#form-option-type-name').val(name)
    modal.find('#form-option-type-id').val(id)
  })

  $('#spu_image_file').change(function(e) {
    console.info(1);
  });

  if ($('#spu_editor').length > 0) {
    var editor = new Simditor({
      textarea: $('#spu_editor'),
      upload: {
        url: '/shop/pictures',
        params: null,
        fileKey: 'picture[image]',
        connectionCount: 3,
        leaveConfirm: '图片正在上传，是否确认离开当前页面?',
      }
    });
  }

  if ($('#term_of_service_editor').length > 0) {
    var editor = new Simditor({
      textarea: $('#term_of_service_editor')
    })
  }

  $('.select').select2();

  $('.leaf').on('click', function(e) {
    e.preventDefault();
    $(this).next().toggle();
  });
  
};

function toggleShopCategoryTree (categoryId) {
  $("tr[data-parent-id=" + categoryId + "]").forEach(element => {
    toggleShopCategoryTree(element.data("id"));
    element.toggle();
  });
};

$(document).on('turbolinks:load', domReady);
